﻿ 
using SHKF.B2C.Model.Entity.WorkingCenter;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace SHKF.B2C.BLL.WorkingCenter
{
    /// <summary>
    /// 分店库存
    /// </summary>
    public class WareHouseDataWarehousBLL : WorkingCenterBLL
    {
        public WareHouseDataWarehousBLL(SqlSugarClient db) : base(db)
        {

        }

        public int Save(WareHouseDataWarehousEntity info)
        {
            int result = 0;
            WareHouseDataWarehousEntity oldInfo = this.db.Queryable<WareHouseDataWarehousEntity>()
                .Where( ww => ww.ShopVSN == info.ShopVSN 
                        && ww.ProductVSN == info.ProductVSN 
                        && ww.WarehousSN == info.WarehousSN)
                .First();

            if( oldInfo != null)
            {
                oldInfo.Qty = info.Qty;
                result = this.db.Updateable<WareHouseDataWarehousEntity>(oldInfo)
                    .ExecuteCommand();
            }
            else
            {
                result = this.db.Insertable<WareHouseDataWarehousEntity>(info)
                    .ExecuteCommand();
            }
            return result;
        }

        /// <summary>
        /// 用于计算分仓下当前品所有库存
        /// </summary>
        /// <param name="ShopVSN"></param>
        /// <param name="ProductVSN"></param>
        /// <returns></returns>
        public int CalcQty(int ShopVSN, string warehousSN, long ProductVSN, int Qty)
        {
            int totalQty = 0;
            try
            {
                this.db.BeginTran();

                //更新分仓
                WareHouseDataWarehousEntity info = this.db.Queryable<WareHouseDataWarehousEntity>()
                    .Where(ww => ww.ShopVSN == ShopVSN && ww.WarehousSN == warehousSN && ww.ProductVSN == ProductVSN)
                    .First();

                if (info != null)
                {
                    info.Qty += Qty;
                    this.db.Updateable<WareHouseDataWarehousEntity>(info)
                        .ExecuteCommand();
                }
                else
                {
                    info.ShopVSN = ShopVSN;
                    info.WarehousSN = warehousSN;
                    info.ProductVSN = ProductVSN;
                    info.Qty = Qty;
                    this.db.Insertable<WareHouseDataWarehousEntity>(info)
                           .ExecuteCommand();
                }

                //所有分仓下的总数　 
                totalQty = db.Queryable<WareHouseDataWarehousEntity>()
                    .Where(ww => ww.ShopVSN == ShopVSN && ww.ProductVSN == ProductVSN)
                    .Sum(it => it.Qty);

                //更新总仓
                int result = 0;
                WareHouseDataAllEntity oldInfo = this.db
                    .Queryable<WareHouseDataAllEntity>()
                    .Where(ww => ww.ShopVSN == info.ShopVSN
                           && ww.ProductVSN == info.ProductVSN)
                    .First();

                if (oldInfo != null)
                {
                    oldInfo.Qty = totalQty;
                    result = this.db.Updateable<WareHouseDataAllEntity>(oldInfo)
                        .ExecuteCommand();
                }
                else
                {
                    result = this.db.Insertable<WareHouseDataAllEntity>(info)
                        .ExecuteCommand();
                }

                this.db.CommitTran();

            }
            catch (Exception ex)
            {
                this.db.RollbackTran();
                throw ex;
            }
            
            return totalQty;


        }
    }
}
